package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.pojo.dto.AliyunCallGetCompanyReqDTO;
import com.xbongbong.paas.pojo.dto.AliyunCallSaveCommunicateReqDTO;
import com.xbongbong.paas.pojo.dto.AliyunCallTimeFilterResultReqDTO;
import com.xbongbong.paas.pojo.dto.AliyunCallUserInfoConditionReqDTO;
import com.xbongbong.paas.pojo.dto.GetSysVisibleDTO;
import com.xbongbong.paas.pojo.vo.AddCommunicateVO;
import com.xbongbong.paas.pojo.vo.AliyunCallUserInfoConditionVO;
import com.xbongbong.paas.pojo.vo.GetSysVisibleVO;
import com.xbongbong.paas.pojo.vo.TimeFilterResultVO;
import com.xbongbong.paas.service.CallCenterCallLogService;
import com.xbongbong.paas.service.CallCenterCommonService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseDTO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.config.ProBaseConfig;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.sys.domain.entity.CompanyEntity;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @Auther: yingjianfeng
 * @Date: 2021/9/6 13:36
 * @Description:
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/common")
public class AliyunCalcenterCommonController {
    @Resource
    private ProBaseConfig proBaseConfig;
    @Resource
    private CallCenterCommonService callCenterCommonService;
    @Resource
    private CallCenterCallLogService callCenterCallLogService;
    /**
     * description: 前端需要个接口拿地址
     * date: 2021/9/2 7:40 下午
     * @Author yingjf
     */
    @RequestMapping(value = "/getUrl", method = RequestMethod.POST)
    public String addAliyunCallCenter(@RequestBody @Valid BaseDTO baseDTO, BindingResult br) {
        XbbResponse<String> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            String url = proBaseConfig.getAliyunCallcenterChartUrl();
            response = new XbbResponse<>(url);
        }
        return JSON.toJSONString(response);
    }

    // 通过号码查询客户
    @RequestMapping(value = "/findUserInfoCondition", method = RequestMethod.POST)
    public String findUserInfoCondition(@RequestBody @Valid AliyunCallUserInfoConditionReqDTO aliyunCallUserInfoConditionReqDTO, BindingResult br) {
        XbbResponse<AliyunCallUserInfoConditionVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                AliyunCallUserInfoConditionVO aliyunCallUserInfoConditionVO = callCenterCallLogService.findUserInfoCondition(aliyunCallUserInfoConditionReqDTO);
                response = new XbbResponse<>(aliyunCallUserInfoConditionVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    // 通过号码查询客户
    @RequestMapping(value = "/saveCommunicate", method = RequestMethod.POST)
    public String saveCommunicate(@RequestBody @Valid AliyunCallSaveCommunicateReqDTO aliyunCallSaveCommunicateReqDTO, BindingResult br) {
        XbbResponse<AddCommunicateVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                AddCommunicateVO addCommunicateVO = callCenterCallLogService.saveCommunicate(aliyunCallSaveCommunicateReqDTO);
                response = new XbbResponse<>(addCommunicateVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    // 获取timeFilter配置
    @RequestMapping(value = "/getTimeFilterResult", method = RequestMethod.POST)
    public String getTimeFilterResult(@RequestBody @Valid AliyunCallTimeFilterResultReqDTO aliyunCallTimeFilterResultReqDTO, BindingResult br) {
        XbbResponse<TimeFilterResultVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TimeFilterResultVO timeFilterResult = callCenterCommonService.getTimeFilterResult(aliyunCallTimeFilterResultReqDTO);
                response = new XbbResponse<>(timeFilterResult);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    // 获取timeFilter配置
    @RequestMapping(value = "/getCompany", method = RequestMethod.POST)
    public XbbResponse<CompanyEntity> getCompany(@RequestBody @Valid AliyunCallGetCompanyReqDTO aliyunCallGetCompanyReqDTO, BindingResult br) {
        XbbResponse<CompanyEntity> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                response = callCenterCommonService.getCompany(aliyunCallGetCompanyReqDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return response;
    }

    @RequestMapping(value = "/getSysVisible", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getSysVisible(@RequestBody @Valid GetSysVisibleDTO getSysVisibleDTO, BindingResult br) {
        XbbResponse<GetSysVisibleVO> response = new XbbResponse<>();
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                GetSysVisibleVO res = callCenterCommonService.getSysVisible(getSysVisibleDTO);
                response = new XbbResponse<>(res);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
